<template>
  <div class="bigBox">
    <div class="left">
      <el-scrollbar
        class="messageContent"
        style="box-sizing: border-box; padding-right: 30px"
        max-height="600px"
      >
        <div
          v-for="(item, index) in messageList"
          :class="item.role === 'assistant' ? 'aiMessage' : 'myMessage'"
        >
          <div v-if="isLoading && index === messageList.length - 1" class="message">
            <img
              style="
                padding: 0 10px;
                width: 20px;
                height: 20px;
                transform: scale(3) translateY(2px);
              "
              src="../../assets/loading.gif"
              alt=""
            />
          </div>
          <div v-else class="message" v-html="md.render(item.content)"></div>

          <!-- From Uiverse.io by Yaya12085 -->
          <div v-if="item.children" style="display: flex">
            <div class="card" v-for="cardItem in item.children">
              <div class="header">
                <div class="image">
                  <img :src="cardItem.avatar" alt="" />
                </div>
                <div>
                  <div class="stars">
                    <svg fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                      <path
                        d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                      ></path>
                    </svg>
                    <svg fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                      <path
                        d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                      ></path>
                    </svg>
                    <svg fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                      <path
                        d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                      ></path>
                    </svg>
                    <svg fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                      <path
                        d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                      ></path>
                    </svg>
                    <svg fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                      <path
                        d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                      ></path>
                    </svg>
                  </div>
                  <p class="name">{{ cardItem.name }}</p>
                </div>
              </div>

              <p class="message">
                {{ cardItem.text }}
              </p>
            </div>
          </div>
        </div>
      </el-scrollbar>
      <div class="inputBox">
        <input
          class="input"
          name="text"
          type="text"
          v-model="input"
          placeholder="请输入你的问题..."
        />
        <button @click="sendMessage">提问</button>
      </div>
    </div>
    <div class="right">
      <div class="notice">注意！该助手仅提供咨询，如果您遇到困难，建议寻找专业律师帮忙！</div>
      <img src="../../assets/image/farmer.png" alt="" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { MessageList } from '@/types/home'
import { ref } from 'vue'
import MarkdownIt from 'markdown-it'
let md: MarkdownIt = new MarkdownIt()

const input = ref('请问如何学习')
const isLoading = ref(false)

const sendMessage = () => {}

const messageList = ref<MessageList>([
  {
    role: 'user',
    content: '请给我推荐几个合适的专家',
  },
  {
    role: 'assistant',
    content: '你好我是你的ai小助手',
    children: [
      {
        name: '张三',
        avatar: 'https://i.pravatar.cc/150?img=1',
        text: '张三，农业经济学博士，专注于农产品供应链优化与市场分析。现任某农业大学教授，曾主持多项国家级农业经济研究项目，发表论文50余篇，尤其在农产品价格波动与农民收入增长领域有深入研究',
      },
      {
        name: '李四',
        avatar: 'https://i.pravatar.cc/150?img=2',
        text: '李四，农产品加工技术专家，拥有20年食品加工行业经验。现任某省级农业科学院研究员，主导开发了多项绿色健康农产品加工技术，成功推动多个地方特色农产品品牌走向全国市场',
      },
      {
        name: '王五',
        avatar: 'https://i.pravatar.cc/150?img=3',
        text: '王五，农业科技推广专家，专注于现代农业技术与数字化农业应用。现任某农业科技公司首席技术官，曾带领团队开发智能农业管理系统，帮助农户提高生产效率，实现精准农业',
      },
    ],
  },
])
</script>

<style lang="scss" scoped>
.bigBox {
  // background-color: red;
  display: flex;

  .left {
    flex: 1;
    margin-right: 20px;
    height: calc(100vh - 200px);
    display: flex;
    flex-direction: column;

    .messageContent {
      padding: 20px 80px;
      flex: 1;
    }

    .aiMessage {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: left;
      margin-bottom: 20px;
      margin-right: 20px;

      .avatar {
        margin-right: 20px;
      }

      .message {
        // flex: 1;
        background: rgba(255, 255, 255, 1);
        // border: 1px solid $primary-border-color;
        background-color: $primary-message-background-color;
        box-sizing: border-box;
        padding: 20px;
        border-radius: 20px 20px 20px 0px;
        margin-bottom: 20px;
        //   border:1px solid $primary-border-color;
      }
    }

    .myMessage {
      margin-bottom: 20px;
      margin-right: 20px;
      display: flex;
      justify-content: right;

      .message {
        background-color: $primary-color;
        box-sizing: border-box;
        padding: 20px;
        color: white;
        margin-left: 70px;
        border-radius: 20px 20px 0 20px;
      }
    }

    .inputBox {
      display: flex;
      margin-left: 70px;
      margin-right: 60px;

      button {
        width: 200px;
        font-size: 18px;
        border-radius: 30px;
        border: none;
        color: white;
        opacity: 1;
        margin-left: 20px;
        background: linear-gradient(142.41deg, rgba(74, 37, 225, 1) 0%, rgba(53, 120, 246, 1) 100%);
        box-shadow: 0px 21px 27px -10px rgba(96, 60, 255, 0.48);
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .input {
        width: 100%;
        //   max-width: 220px;
        height: 30px;
        padding: 12px;
        border-radius: 20px;
        border: 1.5px solid lightgrey;
        outline: none;
        transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
        box-shadow: 0px 0px 20px -18px;
      }

      .input:hover {
        border: 2px solid lightgrey;
        box-shadow: 0px 0px 20px -17px;
      }

      .input:active {
        transform: scale(0.95);
      }

      .input:focus {
        border: 2px solid grey;
      }
    }
  }

  .right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-top: 20px;

    .notice {
      width: 300px;
      // flex: 1;
      height: 300px;
      box-sizing: border-box;
      padding: 20px;
      box-shadow:
        rgba(17, 17, 26, 0.1) 0px 4px 16px,
        rgba(17, 17, 26, 0.05) 0px 8px 32px;

      border-radius: 20px;
    }

    img {
      width: 300px;
      transform: scale(1.5);
    }
  }
}
/* From Uiverse.io by Yaya12085 */
.card {
  background-color: rgba(243, 244, 246, 1);
  padding: 20px;
  max-width: 200px;
  border-radius: 10px;
  box-shadow: 0 20px 30px -20px rgba(5, 5, 5, 0.24);
  margin-right: 20px;
}

.header {
  display: flex;
  align-items: center;
  grid-gap: 1rem;
  gap: 1rem;
}

.header {
  img {
    height: 4rem;
    width: 4rem;
    border-radius: 9999px;
    object-fit: cover;
    background-color: royalblue;
  }
}

.stars {
  display: flex;
  justify-content: center;
  grid-gap: 0.125rem;
  gap: 0.125rem;
  color: rgba(34, 197, 94, 1);
}

.stars svg {
  height: 1rem;
  width: 1rem;
}

.name {
  margin-top: 0.25rem;
  font-size: 1.125rem;
  line-height: 1.75rem;
  font-weight: 600;
  --tw-text-opacity: 1;
  color: rgba(55, 65, 81, 1);
}

.message {
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  margin-top: 1rem;
  color: rgba(107, 114, 128, 1);
}
</style>
